package org.limit.bookServe.service.impl;

import org.limit.bookServe.bean.Book;
import org.limit.bookServe.service.BookService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.limit.bookServe.bean.Book.BookStatusEnum.*;
import static org.limit.bookServe.utils.BaseClass.println;
import static org.limit.bookServe.utils.SystemData.saveBooksData;
import static org.limit.bookServe.utils.SystemData.saveBooksData2Root;

/**
 * @author 1.
 * @version 1.0
 * @description: 书籍服务实现类
 * @date 2023/11/1 20:10
 */
public class BookServiceImpl implements BookService {

    private List<Book> books;

    public BookServiceImpl(List<Book> books) {
        this.books = books;
    }

    @Override
    public void insertBook(Book book) {
        if (containerBook(book.getBookName())) {
            //插入缓存桶
            books.add(book);
            //持久化
            saveBooksData();
        }
    }

    @Override
    public void findBookOne(String bookName) {
        if (!Objects.equals(bookName, "")) {
            books.forEach(book -> {
                if (Objects.equals(bookName, book.getBookName())) {
                    println(String.valueOf(book));
                }
            });
            if (containerBook(bookName)) println("未查找到相关书籍！");
        } else {
            println("书名不规范！");
        }

    }

    //判断书籍是否存在 by bookName
    public boolean containerBook(String bookName) {
        return books.stream().noneMatch(book -> Objects.equals(book.getBookName(), bookName));
    }

    //判断书籍是否存在 by bookId
    public boolean containerBook(int bookDemo) {
        return books.stream().anyMatch(book -> Objects.equals(book.getBid(), bookDemo));
    }

    @Override
    public void deletedBookOne(int bookDemo) {
        if (containerBook(bookDemo)) {
            //过滤要删除的书籍
            List<Book> newBookList = books.stream().filter(book -> book.getBid() != bookDemo).toList();
            //TODO 避免 java.lang.UnsupportedOperationException 异常
            List<Book> list = new ArrayList<>(newBookList);
            //修改缓存并持久化
            books = list;
            saveBooksData2Root(list);
            println("删除成功！");
        }
    }

    @Override
    public void showAllBooks() {
        if (books != null && books.size() > 0) {
            books.forEach(book -> println(book.showCurrBookInfo()));
        }
    }

    @Override
    public void borrowBookOne(String bookName) {
        if (containerBook(bookName)) {
            println("未查找到相关书籍！");
            return;
        }
        books.forEach(book -> {
            if (Objects.equals(bookName, book.getBookName())) {
                if (book.getStatus() != DOWN_LINE && book.getNumber() > 0) {
                    //借阅书籍
                    book.borrowBookFuc();
                    //持久化
                    saveBooksData();
                } else {
                    println("当前书籍" + bookName + book.getStatusDesc());
                }
            }
        });
    }

    @Override
    public void returningBook(String bookName) {
        if (containerBook(bookName)) {
            println("未查找到当前书籍！");
            return;
        }
        books.forEach(book -> {
            if (Objects.equals(bookName, book.getBookName())) {
                if (book.getStatus() != DOWN_LINE && book.getStatus() == BORROWED) {
                    //归还书籍
                    book.return2BookList();
                    //持久化数据
                    saveBooksData();
                } else {
                    println("当前书籍" + bookName + book.getStatusDesc());
                }
            }
        });
    }
}
